﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using ZLDC.Common.BaseUser;

namespace ZLDC.Topo.BLL
{
    public class zlhqDeptTree
    {
        private string sTopDeptId = string.Empty;
        private string sLimitDeptId = string.Empty;
        protected System.Text.StringBuilder sbDeptHtml = new System.Text.StringBuilder();
        private string sKeyName = "zldclpgl-DeptTree-";
        public zlhqDeptTree()
        {

        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="_sTopId"></param>
        /// <param name="_limitDept"></param>
        public zlhqDeptTree(string _sTopId, string _limitDept)
        {
            sTopDeptId = _sTopId;
            sLimitDeptId = _limitDept;
            sKeyName += _sTopId + "_" + _limitDept;
        }

        /// <summary>
        /// 移除部门树缓存

        /// </summary>
        public void ClearCache()
        {
            //System.Web.HttpRuntime.Close();//
            System.Collections.IDictionaryEnumerator iCache = System.Web.HttpRuntime.Cache.GetEnumerator();
            //
            //System.Threading.Thread.Sleep(2000);
            //
            while (iCache.MoveNext())
            {
                if (iCache.Key.ToString().IndexOf(sKeyName) > -1 && System.Web.HttpRuntime.Cache[iCache.Key.ToString()] != null)
                {
                    System.Web.HttpRuntime.Cache.Remove(iCache.Key.ToString());
                }
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sId"></param>
        /// <returns></returns>
        public string GetSonDept(string sId)
        {
            sKeyName += "son_" + sId;
            string sSonDeptHtml = string.Empty;

            if (System.Web.HttpContext.Current.Cache[sKeyName] != null)
            {
                sSonDeptHtml = (string)System.Web.HttpContext.Current.Cache[sKeyName];
            }
            else
            {
                System.Text.StringBuilder sbDeptHtml = new System.Text.StringBuilder();
                DataSet dsDep = Dept.GetDeptData();
                System.Data.DataView dvTree = new System.Data.DataView(dsDep.Tables[0]);
                //过滤ParentID,得到当前的所有子节点
                dvTree.RowFilter = "[parent_id] = " + sId;
                if (dvTree.Count > 0)
                {
                    foreach (System.Data.DataRowView datav in dvTree)
                    {
                        if (GetExistsDeptSon(datav["id"].ToString(), dsDep))
                        {
                            //存在下级
                            sbDeptHtml.Append("<li id='" + datav["id"].ToString() + "_1' ><span><a onclick='jt(" + datav["id"].ToString() + ",\"" + datav["name"].ToString() + "\");'>" + datav["name"].ToString() + "</a></span>");
                            sbDeptHtml.Append("<ul class='ajax'>");
                            sbDeptHtml.Append("<li id='" + datav["id"].ToString() + "'>{url:../../AjaxData/GetDeptTree.aspx?id=" + datav["id"].ToString() + "}</li>");
                            sbDeptHtml.Append("</ul>");
                            sbDeptHtml.Append("</li>");
                        }
                        else
                        {
                            //不存在下级
                            sbDeptHtml.Append("<li id='" + datav["id"].ToString() + "'><span class='text'><a onclick='jt(" + datav["id"].ToString() + ",\"" + datav["name"].ToString() + "\");'>" + datav["name"].ToString() + "</a></span></li>");
                        }
                    }
                }
                sSonDeptHtml = sbDeptHtml.ToString();
                System.Web.HttpContext.Current.Cache.Insert(sKeyName, sSonDeptHtml, null, DateTime.Now.AddMinutes(15), TimeSpan.Zero);
            }
            return sSonDeptHtml;
        }

        /// <summary>
        /// 取得管理信息系统中部门树
        /// </summary>
        /// <returns></returns>
        public string GetZlhqDept()
        {
            if (System.Web.HttpContext.Current.Cache[sKeyName] != null)
            {
                sbDeptHtml = (System.Text.StringBuilder)System.Web.HttpContext.Current.Cache[sKeyName];
            }
            else
            {
                string sSql = "select id,name,grade,parent_id from t_s_alldept where dltflg = 0 ";
                if (sLimitDeptId != string.Empty)
                {
                    sSql += " and id in (" + sLimitDeptId + ") ";
                }
                if (sTopDeptId == string.Empty)
                {
                    //取得顶层部门编号
                    sSql = "select top 1 * from (" + sSql + ") tt where grade is not null order by grade";
                    DataBaseAll db = new DataBaseAll("ZLDCHR");
                    DataSet ds = db.SelectDataBaseAll(sSql);
                    
                    if (ds.Tables[0].Rows.Count > 0)
                    {
                        sTopDeptId = ds.Tables[0].Rows[0]["id"].ToString();
                    }
                    else
                    {
                        sTopDeptId = "-1";
                    }
                }
                AddTree(sTopDeptId, Dept.GetDeptData());
                System.Web.HttpContext.Current.Cache.Insert(sKeyName, sbDeptHtml, null, DateTime.Now.AddMinutes(15), TimeSpan.Zero);
            }
            //
            return sbDeptHtml.ToString();
        }

        /// <summary>
        /// 顶层部门
        /// </summary>
        /// <param name="ID"></param>
        /// <param name="dsDep"></param>
        protected void AddTree(string ID, DataSet dsDep)
        {
            System.Data.DataView dvTree = new System.Data.DataView(dsDep.Tables[0]);
            //过滤ParentID,得到当前的所有子节点
            dvTree.RowFilter = "[id] = " + ID;
            sbDeptHtml.Append("<ul class='simpleTree'>\n");
            foreach (System.Data.DataRowView datav in dvTree)
            {
                sbDeptHtml.Append("<li class='root' id='" + datav["id"].ToString() + "'><span><a onclick='jt(" + datav["id"].ToString() + ",\"" + datav["name"].ToString() + "\");'>" + datav["name"].ToString() + "</a></span>\n");
                AddTree2(datav["id"].ToString(), dsDep, string.Empty);
                
                sbDeptHtml.Append("</li>\n");
            }
            sbDeptHtml.Append("</ul>\n");
        }

        /// <summary>
        /// 第二级
        /// </summary>
        protected void AddTree2(string ParentID, System.Data.DataSet dsDep, string sTmpTemplate)
        {
            System.Data.DataView dvTree = new System.Data.DataView(dsDep.Tables[0]);
            //过滤ParentID,得到当前的所有子节点
            dvTree.RowFilter = "[parent_id] = " + ParentID;
            if (dvTree.Count > 0)
            {
                sbDeptHtml.Append("<ul>\n");
                foreach (System.Data.DataRowView datav in dvTree)
                {
                    sbDeptHtml.Append("<li id='" + datav["id"].ToString() + "'><span><a onclick='jt(" + datav["id"].ToString() + ",\"" + datav["name"].ToString() + "\");'>" + datav["name"].ToString() + "</a></span>\n");
                    AddTree3(datav["id"].ToString(), dsDep, string.Empty);
                    sbDeptHtml.Append("</li>\n");
                }
                sbDeptHtml.Append("</ul>\n");
            }
        }

        /// <summary>
        /// 从第三级开始无刷新取
        /// </summary>
        protected void AddTree3(string ParentID, System.Data.DataSet dsDep, string sTmpTemplate)
        {
            System.Data.DataView dvTree = new System.Data.DataView(dsDep.Tables[0]);
            //过滤ParentID,得到当前的所有子节点
            dvTree.RowFilter = "[parent_id] = " + ParentID;
            if (dvTree.Count > 0)
            {
                sbDeptHtml.Append("<ul class='ajax'>\n");
                sbDeptHtml.Append("<li id='" + ParentID + "'>{url:../../AjaxData/GetDeptTree.aspx?id=" + ParentID + "}</li>\n");
                sbDeptHtml.Append("</ul>\n");
            }
        }

        /// <summary>
        /// 是否存在下级部门
        /// </summary>
        private bool GetExistsDeptSon(string dpid, DataSet ds)
        {
            System.Data.DataView dvTree = new System.Data.DataView(ds.Tables[0]);
            //过滤ParentID,得到当前的所有子节点
            dvTree.RowFilter = "[parent_id] = " + dpid;
            if (dvTree.Count > 0)            
                return true;            
            else            
                return false;            
        }

    }
}
